﻿<template>
  <div id="app" :class="{ 'dark-theme': isDarkTheme }">
    <DefaultLayout v-if="appInitialized" />
    <AppLoading v-else />
    
    <!-- 全局错误提示 -->
    <el-dialog
      v-model="showErrorDialog"
      title="系统错误"
      width="500px"
      :close-on-click-modal="false">
      <div class="error-content">
        <el-alert
          :title="errorMessage"
          type="error"
          :description="errorCode ? `错误代码: ${errorCode}` : ''"
          show-icon
          :closable="false" />
      </div>
      <template #footer>
        <el-button @click="reloadApp">重新加载</el-button>
        <el-button type="primary" @click="clearError">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, onMounted, watch } from 'vue'
import { useGlobalStore } from './stores/globalStore'
import DefaultLayout from './layouts/DefaultLayout.vue'
import AppLoading from './components/common/AppLoading.vue'

// 全局状态
const globalStore = useGlobalStore()

// 计算属性
const appInitialized = computed(() => globalStore.appInitialized)
const isDarkTheme = computed(() => globalStore.isDarkTheme)
const hasError = computed(() => globalStore.hasError)
const errorMessage = computed(() => globalStore.errorMessage)
const errorCode = computed(() => globalStore.errorCode)
const showErrorDialog = computed({
  get: () => hasError.value,
  set: (value) => {
    if (!value) {
      globalStore.clearError()
    }
  }
})

// 方法
const clearError = () => {
  globalStore.clearError()
}

const reloadApp = () => {
  window.location.reload()
}

// 监听主题变化
watch(isDarkTheme, (isDark) => {
  document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light')
}, { immediate: true })

// 应用初始化
onMounted(async () => {
  try {
    await globalStore.initializeApp()
  } catch (error) {
    console.error('应用初始化失败:', error)
    globalStore.setError('应用初始化失败，请刷新页面重试', error.code)
  }
})

// 全局错误处理
// window.addEventListener('error', (event) => {
//   console.error('全局错误:', event.error)
//   globalStore.setError('发生未知错误', 'UNKNOWN_ERROR')
// })

// window.addEventListener('unhandledrejection', (event) => {
//   console.error('未处理的Promise拒绝:', event.reason)
//   globalStore.setError('操作失败，请重试', 'PROMISE_REJECTION')
// })
</script>

<style>
/* 全局样式重置 */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#app {
  height: 100vh;
  overflow: hidden;
  transition: all 0.3s ease;
}

/* CSS变量定义 */
:root {
  /* 浅色主题 */
  --app-bg-color: #ffffff;
  --app-text-color: #303133;
  --app-border-color: #dcdfe6;
  --app-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
  /* 深色主题 */
  --app-bg-color: #1a1a1a;
  --app-text-color: #e5eaf3;
  --app-border-color: #414243;
  --app-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.3);
}

/* 深色主题类 */
.dark-theme {
  background-color: var(--app-bg-color);
  color: var(--app-text-color);
}

/* 滚动条样式 */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--el-fill-color-lighter);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb {
  background: var(--el-fill-color-dark);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--el-fill-color-darker);
}

/* 选择文本样式 */
::selection {
  background-color: var(--el-color-primary);
  color: white;
}

/* 焦点样式 */
:focus-visible {
  outline: 2px solid var(--el-color-primary);
  outline-offset: 2px;
}

/* 动画类 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: transform 0.3s ease;
}

.slide-enter-from {
  transform: translateX(-100%);
}

.slide-leave-to {
  transform: translateX(100%);
}

/* 工具类 */
.text-center {
  text-align: center;
}

.text-left {
  text-align: left;
}

.text-right {
  text-align: right;
}

.flex {
  display: flex;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.full-width {
  width: 100%;
}

.full-height {
  height: 100%;
}

.no-select {
  user-select: none;
}

.pointer {
  cursor: pointer;
}

/* 错误内容样式 */
.error-content {
  margin: 16px 0;
}

/* 响应式断点 */
@media (max-width: 768px) {
  #app {
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  #app {
    font-size: 12px;
  }
}

/* 打印样式 */
@media print {
  #app {
    background: white !important;
    color: black !important;
  }
  
  .no-print {
    display: none !important;
  }
}
</style>